package cn.com.shopec.erp.system.controller;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.common.utils.JsonHelper2;
import cn.com.shopec.erp.core.base.controller.BaseController;
import cn.com.shopec.erp.product.controller.CommodityCatController;
import cn.com.shopec.erp.system.service.IParamsService;
import cn.com.shopec.erp.system.vo.ParameterVo;

/**
 * 
 * @ClassName ParameterController
 * @Description 参数配置Controller
 * @author Liang Shun Wang
 * @date 2016-11-30 上午10:58:18
 */
@Controller
@RequestMapping("/manager/system/parameter")
public class ParameterController extends BaseController{
	
	private static Log log = LogFactory.getLog(CommodityCatController.class);
	
	@Resource
	private IParamsService iparamsService;
	
	/**
	 * 
	 * @MethodName parameterList
	 * @Description 系统参数配置列表
	 * @author Liang Shun Wang
	 * @date 2016-11-30 下午4:29:26
	 * @param Parameter
	 * @param modelMap
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("/parameterList")
	public ModelAndView  parameterList(ParameterVo parameterVo,ModelMap map) throws Exception{
		map.put("paramterVo",parameterVo);
		return new ModelAndView("system/params/parameter_List");
	}
	
	/**
	 * 
	 * @MethodName ajaxToRoleList
	 * @Description ajax分页查询系统参数配置列表
	 * @author chengui
	 * @date 2016-11-30 下午4:47:03
	 * @param roleVo
	 * @param pageQuery
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping("/ajaxToParamsList")
	@ResponseBody
	public String ajaxToParamList(ParameterVo parameterVo, PageQuery pageQuery,ModelMap map) throws Exception {
		try {
			PageFinder<ParameterVo> pageFind = iparamsService.getPagedParamsVos(parameterVo, pageQuery);
            map.put("Rows", pageFind.getData());
            map.put("Total", pageFind.getRowCount());
        } catch (Exception e) {
            log.error("listBrand:" + e.getMessage(), e);
        }
        return JsonHelper2.toJson(map);
	}
	
	/**
     * 
     * @Title: toAddBrand
     * @Description: 跳转到添加系统参数配置页面
     * @return
     * @throws
     * @author: LiangShunWang
     * @date: 2016-11-21下午19:21:57
     */
    @RequestMapping("/to_add_parameter")
    public String toAddParameter(PageQuery pageQuery,ModelMap map) {
        return "/system/params/parameter_add";
    }
    
    /**
     * 
     * @Title: addParamet
     * @Description: 添加参数值
     * @param brandVo
     * @param map
     * @return
     * @throws Exception
     * @throws
     * @author: LiangShunWang
     * @date: 2016-11-21下午19:16:58
     */
    @RequestMapping("/add_parameter")
    @ResponseBody
    public String addParamet(ParameterVo parameterVo, ModelMap map) throws Exception{
        try {
            this.iparamsService.addParameter(parameterVo);
        } catch (Exception e) {
            log.error("addBrand:" + e.getMessage(), e);
            return SZSCMConstant.FAIL;
        }
        return SZSCMConstant.SUCCESS;
    }
    
    /**
     * 
     * @Title: toUpdateParameter
     * @Description: 获取系统参数修改页面所需参数
     * @param id
     * @param map
     * @return
     * @throws Exception
     * @throws
     * @author: LiangShunWang
     * @date: 2016-11-21下午19:16:58
     */
    @RequestMapping("/to_update_parameter")
    public String toUpdateParameter(String id,ModelMap map){
    	try{
    		ParameterVo parameterVo =  this.iparamsService.getParameterVo(id);
    		map.put("parameterVo", parameterVo);
    	}catch(Exception e){
    		log.error("toUpdateBrand:" + e.getMessage(), e);
    	}
    	return "/system/params/parameter_update";
    }
    
    /**
     * 
     * @Title: updateBrand
     * @Description: 修改次品信息
     * @param brandVo
     * @param map
     * @return
     * @throws
     * @author: LiangShunWang
     * @date: 2013-5-27下午02:48:51
     */
    @RequestMapping("/update_parameter")
    @ResponseBody
    public String updateParameter(ParameterVo parameterVo, ModelMap map) {
        try {
        	this.iparamsService.updateParameter(parameterVo);
        } catch (Exception e) {
            log.error("update_defective:" + e.getMessage(), e);
            return SZSCMConstant.FAIL;
        }
        return SZSCMConstant.SUCCESS;
    }
    
    /**
     * 
     * @Title: deleteDefective
     * @Description: 删除系统参数
     * @param brandVo
     * @return
     * @throws
     * @author: LiangShunWang
     * @date: 2016-11-30下午20:48:51
     */
    @RequestMapping("/deleteParamter")
    @ResponseBody
    public String deleteParamter(ParameterVo parameterVo) {
        try {
            this.iparamsService.delParameter(parameterVo.getId());
            return SZSCMConstant.SUCCESS;
        } catch (Exception e) {
            log.error("deleteBrand:" + e.getMessage(), e);
            return SZSCMConstant.FAIL;
        }
    }

}
